﻿#include "logindialog.h"
#include "ui_logindialog.h"

#include "protocolmessage.h"
#include "zhidebug.h"
#include "zhistringutil.h"

#include <QMessageBox>

LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    this->resize(358, 151);
    this->setWindowTitle(tr("QtCMS"));

    uimgLabel = new QLabel;
    unameLineEdit = new QLineEdit;
    upassLineEdit = new QLineEdit;

    hostCombox = new QComboBox;

    loginButton = new QPushButton;
    exitButton = new QPushButton;

    loginButton->setFixedHeight(30);
    exitButton->setFixedHeight(30);
    unameLineEdit->setFixedWidth(210);
    upassLineEdit->setFixedWidth(210);
    hostCombox->setFixedWidth(210);

    QPixmap pixmap(":/images/default/avatar");
    uimgLabel->setFixedSize(120, 120);
    uimgLabel->setPixmap(pixmap);
    uimgLabel->setScaledContents(true);

    unameLineEdit->setPlaceholderText(tr("用户名"));
    upassLineEdit->setPlaceholderText(tr("密码"));
    upassLineEdit->setEchoMode(QLineEdit::Password);

    hostCombox->addItem("192.168.0.106");
    hostCombox->addItem("127.0.0.1");
    hostCombox->addItem("192.168.142.152");

    loginButton->setText(tr("登 录"));
    exitButton->setText(tr("退 出"));

    QWidget *mainWidget = new QWidget(this);
    mainLayout = new QGridLayout(mainWidget);

    //this->setCentralWidget(mainWidget);

    mainLayout->addWidget(uimgLabel, 0, 0, 4, 1);
    mainLayout->addWidget(unameLineEdit, 0, 1, 1, 2);
    mainLayout->addWidget(upassLineEdit, 1, 1, 1, 2);
    mainLayout->addWidget(hostCombox, 2, 1, 1, 2);
    mainLayout->addWidget(loginButton, 3, 1, 1, 1);
    mainLayout->addWidget(exitButton, 3, 2, 1, 1);
    //mainLayout->setHorizontalSpacing(10);
    //mainLayout->setVerticalSpacing(10);
    //mainLayout->setContentsMargins(10, 10, 10, 10);

    this->setLayout(mainLayout);

    connect(loginButton, &QPushButton::clicked, this, &LoginDialog::login);
    connect(exitButton, &QPushButton::clicked, this, &LoginDialog::close);

}

void LoginDialog::login()
{
    loginButton->setDisabled(true);
    exitButton->setDisabled(true);
    QString host = hostCombox->currentText();
    clientSocket = new ClientSocket(host, 8000);
    connect(clientSocket, SIGNAL(receiveData(QString)), this, SLOT(receiveData(QString)));
    connect(clientSocket, SIGNAL(connected()), this, SLOT(sendLoginData()));
}

void LoginDialog::sendLoginData()
{
    QString username = unameLineEdit->text().trimmed();
    QString password = upassLineEdit->text().trimmed();
    clientSocket->sendValidateLoginData(username, password);
}

void LoginDialog::receiveData(QString msg)
{
    ZhiDebug::Info("Begin analysis data from server! data is \"" + msg + "\"");
    ProtocolMessage *protocolMessage = new ProtocolMessage(msg);
    if(protocolMessage->category == ProtocolMessage::VALIDATA)
    {
        if(ZhiStringUtil::isTrue(protocolMessage->data.value("result"))){
            QMessageBox::information(this,tr("Info"),tr("Success to login!"),QMessageBox::Ok);
            accept();
        }else{
            QMessageBox::information(this,tr("Info"),protocolMessage->data.value("message"),QMessageBox::Ok);
        }
    }
    if(clientSocket->tcpSocket->state() == QAbstractSocket::ConnectedState){
        clientSocket->tcpSocket->disconnect();
    }
    delete clientSocket;
    clientSocket = nullptr;
    loginButton->setDisabled(false);
    exitButton->setDisabled(false);

}
LoginDialog::~LoginDialog()
{
    delete ui;
}
